package com.bio.utils;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import com.bio.entity.user.User;

public class Environment implements Serializable {

    private static final long serialVersionUID = -6418125174181832828L;
    
    public static HttpSession getSession(){
    	HttpServletRequest request = ServletActionContext.getRequest();
    	if(null != request){
    		return request.getSession();
    	}
    	return null;
    }
    
    public static void initialize(User user) {
    	HttpSession session = getSession();
    	if(null != session){
    		session.setAttribute(Constants.Session.LOGIN_USER, user);
        	session.setMaxInactiveInterval(1200);
    	}
    }

    public static class Session implements Serializable {
        private static final long serialVersionUID = -4618463277316385860L;
        
        public static Long getUserId() {
        	HttpSession session = getSession();
        	if(null == session){
        		return null;
        	}
        	Object object = session.getAttribute(Constants.Session.LOGIN_USER);
            return object == null ? null : ((User)object).getId();
        }
        
        public static String getUserName() {
        	HttpSession session = getSession();
        	if(null == session){
        		return null;
        	}
        	Object object = session.getAttribute(Constants.Session.LOGIN_USER);
            return object == null ? null : ((User)object).getUserName();
        }
        
        public static Long getCompanyId() {
        	HttpSession session = getSession();
        	if(null == session){
        		return null;
        	}
        	Object object = session.getAttribute(Constants.Session.LOGIN_USER);
            return object == null ? null : ((User)object).getCompanyId();
        }
        
        public static User getUser(){
        	HttpSession session = getSession();
        	if(null == session){
        		return null;
        	}
        	Object object = session.getAttribute(Constants.Session.LOGIN_USER);
            return object == null ? null : (User)object;
        }
        
    }
}
